package com.content.scaffold.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.content.scaffold.common.exception.DataModifyException;
import com.content.scaffold.common.identity.IdGenerator;
import com.content.scaffold.common.interceptor.SensitiveInterceptor;
import com.content.scaffold.common.handler.AesEncryptHandler;
import com.content.scaffold.common.mybatis.CustomMetaObjectHandler;
import com.content.scaffold.common.properties.EncryptProperties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author xiaojl
 */
@Configuration
public class MybatisConfig {
    @Resource
    private IdGenerator idGenerator;
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new CustomMetaObjectHandler(idGenerator);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        //乐观锁插件
        OptimisticLockerInnerInterceptor lockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        lockerInnerInterceptor.setException(new DataModifyException());
        interceptor.addInnerInterceptor(lockerInnerInterceptor);


        return interceptor;
    }

    @Bean
    public SensitiveInterceptor sensitiveInterceptor() {
        return new SensitiveInterceptor();
    }

    // @Bean
    public AesEncryptHandler aesEncryptHandler(EncryptProperties properties) {
        // 注入加密处理器:ml-citation{ref="6" data="citationList"}
      //  return new AesEncryptHandler(properties);
        return null;
    }

}
